﻿using System;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
class Program
{
    static void Main(string[] args)
    {
        var cts = new CancellationTokenSource();
        var ct = cts.Token;

        Task task1 = new Task(() => { Run1(ct); }, ct);

        Task task2 = new Task(Run2);

        try
        {
            task1.Start();
            task2.Start();

            Thread.Sleep(1000);

            cts.Cancel();

            Task.WaitAll(task1, task2);
        }
        catch (AggregateException ex)
        {
            foreach (var e in ex.InnerExceptions)
            {
                Console.WriteLine("\nhi,我是OperationCanceledException：{0}\n", e.Message);
            }

            //task1是否取消
            Console.WriteLine("task1是不是被取消了？ {0}", task1.IsCanceled);
            Console.WriteLine("task2是不是被取消了？ {0}", task2.IsCanceled);
        }

        Console.Read();
    }

    static void Run1(CancellationToken ct)
    {
        ct.ThrowIfCancellationRequested();

        Console.WriteLine("我是任务1");

        Thread.Sleep(2000);

        ct.ThrowIfCancellationRequested();

        Console.WriteLine("我是任务1的第二部分信息");
    }

    static void Run2()
    {
        Console.WriteLine("我是任务2");
    }
}